//
//  ViewController.m
//  T_TableViewDemo
//
//  Created by CRY on 16/6/27.
//  Copyright © 2016年 CRY. All rights reserved.
//

#import "ViewController.h"
#import "UITableView+Refresh.h"

@interface ViewController ()<UITableViewDelegate, UITableViewDataSource, RefreshDelegate>

@property (nonatomic, strong) UITableView    *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation ViewController
{
    BOOL _isLoadNewData;
    BOOL _reset;
    NSInteger _offSet;
    NSInteger _rows;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    _offSet = 0;
    _rows   = 15;
    _dataArray = [NSMutableArray array];
    float screen_W   = self.view.bounds.size.width;
    float screen_H   = self.view.bounds.size.height;
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screen_W, 64)];
    view.backgroundColor = [UIColor grayColor];
    [self.view addSubview:view];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, screen_W, screen_H - 64) style:UITableViewStylePlain];
    _tableView.delegate        = self;
    _tableView.dataSource      = self;
    _tableView.refreshDelegate = self;
    _tableView.rowHeight       = 80.0;
    
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"id"];
    [self.view addSubview:_tableView];
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame     = CGRectMake(screen_W - 84, screen_H - 84, 64, 64);
    button.layer.cornerRadius  = 32.0;
    button.layer.masksToBounds = YES;
    [button setTitle:@"重置" forState:UIControlStateNormal];
    button.backgroundColor     = [UIColor grayColor];
    [button addTarget:self action:@selector(onClickBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}

- (void)onClickBtn:(UIButton *)btn{
    
    _reset = YES;
    [_tableView beginRefreshing];
    
    float screen_W   = self.view.bounds.size.width;
    float screen_H   = self.view.bounds.size.height;
    
    float orgin_X = 0;
    if (btn.frame.origin.x == screen_W - 84) {
        orgin_X = 84;
    }else{
        orgin_X = screen_W - 84;
    }
    
    [UIView animateWithDuration:0.9 delay:0 usingSpringWithDamping:0.45 initialSpringVelocity:0 options:0 animations:^{
        
        btn.frame = CGRectMake(orgin_X, screen_H - 84, 64, 64);
    } completion:^(BOOL finished) {}];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"id" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1];
    cell.textLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row];
    return cell;
}
// 将要下拉刷新
- (void)tableViewWillRefreshHeader:(UITableView *)tableView{
    
    _isLoadNewData = YES;
    _offSet = 0;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self parseData];
    });
    
}
// 将要上拉加载
- (void)tableViewWillRefreshFooter:(UITableView *)tableView{
    _isLoadNewData = NO;
    _offSet = _dataArray.count;
    [self parseData];
}
// 解析数据
- (void)parseData{
    
    [_tableView endRefreshing];
    if (_isLoadNewData) {
        [_dataArray removeAllObjects];
    }
    if (_dataArray.count < 40) {
        
        for (int i = 0; i < _rows; i++) {
            [_dataArray addObject:@""];
        }
    }
    // 模拟请求到空数据
    if (_reset) {
        [_dataArray removeAllObjects];
        _reset = NO;
    }
    
    [_tableView reloadData];
    [_tableView resetFooterWithCountOfResponse:_dataArray.count - _offSet
                              countOfdataArray:_dataArray.count
                                          rows:_rows];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
